#!/bin/bash
#author 51yuki
#description install fastdfs-nginx-module
yum -y install unzip zip &>/dev/null
Version=$1
SoftDir=/usr/local/src
InstallDir=/data/application/
cd ${InstallDir}/nginx/sbin &&
Compile_Parameters="$(./nginx -V 2>&1 |  grep configure | awk "{\$1=\$2=\"\";print \$0}")"
[ ! -d /etc/fdfs ] && mkdir /etc/fdfs
if [ $# -ne 1 ];then
	echo "Usage: $0 <nginx Version>"
	exit 2
fi
function install_fastdfs() {
	cd $SoftDir && wget https://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz && tar xf fastdfs-nginx-module_v1.16.tar.gz 
	ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
	ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
	ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
	ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so 
	cd ${SoftDir}/nginx-${Version} && ./configure ${Compile_Parameters} --add-module=/usr/local/src/fastdfs-nginx-module/src && make 
	if [ $? -eq 0 ];then
		cp ${SoftDir}/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
		cp /etc/fdfs/mod_fastdfs.conf{,.ori}
	         systemctl stop nginx.service && cd ${InstallDir}/nginx/sbin && mv nginx nginx.ori_$(date +%F)
                cd ${SoftDir}/nginx-${Version} && cp objs/nginx ${InstallDir}/nginx/sbin/ && systemctl start nginx.service
	fi
}
install_fastdfs

